package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.SpuImage;
import com.atguigu.gulimall.product.entity.SpuInfo;
import com.atguigu.gulimall.product.entity.SpuSaleAttr;
import com.atguigu.gulimall.product.entity.SpuSaleAttrValue;
import com.atguigu.gulimall.product.mapper.SpuInfoMapper;
import com.atguigu.gulimall.product.service.SpuImageService;
import com.atguigu.gulimall.product.service.SpuInfoService;
import com.atguigu.gulimall.product.service.SpuSaleAttrService;
import com.atguigu.gulimall.product.service.SpuSaleAttrValueService;
import com.atguigu.gulimall.product.vo.SpuSaveInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author nicc
 * @description 针对表【spu_info(商品表)】的数据库操作Service实现
 * @createDate 2022-09-29 16:18:40
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
        implements SpuInfoService{

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    @Override
    public Page<SpuInfo> getSpuPage(Integer page, Integer limit, Long category3Id){
        Page<SpuInfo> infoPage = new Page<>(page, limit);

        QueryWrapper<SpuInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id", category3Id);

        Page<SpuInfo> spuInfoPage = this.page(infoPage, queryWrapper);

        return spuInfoPage;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuSaveInfoVo spuSaveInfoVo){

        //1.保存spuInfo
        SpuInfo spuInfo = new SpuInfo();

        //复制属性值到spuInfo
        BeanUtils.copyProperties(spuSaveInfoVo, spuInfo);
        this.save(spuInfo);

        //获取spuId
        Long spuId = spuInfo.getId();

        //2.保存图片信息
        List<SpuImage> spuImages = spuSaveInfoVo.getSpuImageList().stream()
                .map(item -> {
                    SpuImage spuImage = new SpuImage();

                    //复制属性值到spuImage
                    BeanUtils.copyProperties(item, spuImage);
                    //回填spuId
                    spuImage.setSpuId(spuId);
                    return spuImage;
                }).collect(Collectors.toList());

        //调用spuImageService批量保存
        spuImageService.saveBatch(spuImages);


        //3.保存spu销售属性列表
        spuSaveInfoVo.getSpuSaleAttrList().stream()
                .forEach(item -> {
                    SpuSaleAttr spuSaleAttr = new SpuSaleAttr();

                    //分钟内改装spuSaleAttr
                    BeanUtils.copyProperties(item, spuSaleAttr);
                    //回填spuId
                    spuSaleAttr.setSpuId(spuId);

                    spuSaleAttrService.save(spuSaleAttr);

                    //获取到销售信息值列表
                    List<SpuSaleAttrValue> attrValues = item.getSpuSaleAttrValueList().stream()
                            .map(val -> {
                                SpuSaleAttrValue value = new SpuSaleAttrValue();

                                BeanUtils.copyProperties(val, value);
                                //回填
                                value.setSpuId(spuId);
                                value.setSaleAttrName(spuSaleAttr.getSaleAttrName());

                                return value;
                            }).collect(Collectors.toList());

                    //批量保存销售属性值
                    spuSaleAttrValueService.saveBatch(attrValues);
                });
    }
}